package com.cdut.logistics.model.dao.system;
import java.util.Date;
import java.util.List;

import com.cdut.logistics.model.entity.system.Character;
import com.jfinal.plugin.activerecord.Model;
/**
 * 
 * @ClassName: characDAO  
 * @Description: TODO(角色类的DAO，用于和数据库交互)  
 * @author lxh
 * @date 2018年1月28日 下午2:43:26
 */
public class characDAO {
	Character character=new Character();
	/**
	 * 
	 * @Title: AddCharac  
	 * @Description: TODO(新增角色)
	 * @author lxh
	 * @param name
	 * @param authority_id
	 * @param is_use
	 * @param active
	 * @return
	 * @throws
	 */
	public boolean addCharac(String name,String authority_id,String is_use,String active,Date created){
		boolean result = false;
		try{
			if(character.findFirst("select * from role where name=?",name)==null){
				character.set("name", name);
				character.set("Authority_id",authority_id);
				character.set("is_use",is_use);
				character.set("active",active);
				character.set("created",created);
				result=character.save();
			}
				
		}catch(Exception e){}
		return result;
	}
	/**
	 * 
	 * @Title: UpdateCharac  
	 * @Description: TODO(修改角色信息)
	 * @author lxh
	 * @param id
	 * @param name
	 * @param authority_id
	 * @param is_use
	 * @param active
	 * @return
	 * @throws
	 */
	public boolean updateCharac(String id,String name, String authority_id, String is_use,Date updated){
		try{
			character=character.findById(id);
			character=character.set("name", name);
			character=character.set("Authority_id",authority_id);
			character=character.set("is_use",is_use);
			character=character.set("updated",updated);
		}catch(Exception e){}
		boolean result=character.update();
		return result;
	}
	/**
	 * 
	 * @Title: QueryCharac  
	 * @Description: TODO(查询角色信息)
	 * @author lxh
	 * @param id
	 * @return
	 * @throws
	 */
	public Character queryCharacById(String id){
		Character character1=new Character();
		try{
		character1=character.findFirst("SELECT role.id AS '角色ID',role.`name` AS '角色名',authority.`name` AS '权限',authority.url FROM role JOIN authority ON role.Authority_id = authority.id WHERE role.id=?",id);
		}catch(Exception e){}
		return character1;
	}
	/**
	 * 
	 * @Title: DeleteCharac  
	 * @Description: TODO(逻辑删除角色)
	 * @author lxh
	 * @param id
	 * @return
	 * @throws
	 */
	public boolean deleteCharac(String id) {
		try{
			character=character.findById(id);
			character.set("active",0);
		}catch(Exception e){}
		boolean result=character.update();
		return result;
	}
	/**
	 * 
	 * @Title: QueryAllCharac  
	 * @Description: TODO(查询所有角色信息)
	 * @author lxh
	 * @return
	 * @throws
	 */
	public List<Character> queryAllCharac() {
		// TODO Auto-generated method stub
		
		List<Character> character1=character.find("SELECT role.id AS '角色id',role.`name` AS '角色名',role.is_use,role.created AS'创建时间',role.updated AS'更新时间',role.remarks AS'评论',role.active,authority.`name` AS '权限名' FROM role JOIN authority ON role.Authority_id = authority.id");
		return character1;
	}
	public Character queryCharacByName(String name) {
		// TODO Auto-generated method stub
		
		character=character.findFirst("SELECT role.id AS '角色ID',role.`name` AS '角色名',authority.`name` AS '权限',authority.url FROM role JOIN authority ON role.Authority_id = authority.id WHERE role.name=?;",name);
		return character;
	}
//SELECT role.id,role.`name`,authority.`name` AS 'autho_name' FROM role JOIN authority ON role.authority_id=authority.id

	//SELECT * FROM role  
//	JOIN authority ON  role.authority_id=authority.id
	
}
